﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;

namespace YouDaoNotesCodes.JSON
{
    class PracticalApplication
    {

        public void PA0()
        {
            //读取本地路径下的json
            string path = AppDomain.CurrentDomain.BaseDirectory + "";//debug下
            //读取指定路径下的json
            string jsonfile = "E://NotesSummary/NecessaryFile/JsonToDataSet.json";

            //读取自定目录下的json文件
            StreamReader sr = new StreamReader(jsonfile);
            string json = sr.ReadToEnd();
            Console.WriteLine(json);
            //json文件转为 对象  T 创建的类 字段名 应该和json文件中的保持一致     
            DataSet ds = JsonConvert.DeserializeObject<DataSet>(json);

            //转换为指定格式的json
            var json1 = Newtonsoft.Json.JsonConvert.SerializeObject(ds);
            //写入到指定文件中
            StreamWriter sw = new StreamWriter(@"D:\new.json");
            sw.Write(json1);
            sw.Flush();
            sw.Close();


        }

        /// <summary>
        /// JSON格式化为实体类格式  str转为ss
        /// 实际应用场景：数据库关联查询数据有主表和明细表数据，DataTable后序列化是一个JSON数组，需要保持一个格式：主表数据N条 + 对应多条明细数据，此时需要构造一个JSON
        /// </summary>
        public void PA1()
        {
            string str = "[{\"WLDWID\":\"gr+pxMTdQEKwuUFfz7Vw6r8MBA4=\",\"WLDWCODE\":\"5101109101\",\"ACCID\":\"DxYAAAR7lXgY1F9r\",\"ACCCODE\":\"777823\"}]";
            string ss = "[{\"WLDWID\":\"gr+pxMTdQEKwuUFfz7Vw6r8MBA4=\",\"WLDWCODE\":\"5101109101\",\"BANKACCLIST\":[{\"ACCID\":\"DxYAAAR7lXgY1F9r\",\"ACCCODE\":\"777823\"}]}]";
            DataTable dt = null;

            var x = dt.AsEnumerable().GroupBy(dr => new { WLDWID = dr["WLDWID"].ToString(), WLDWCODE = dr["WLDWCODE"].ToString() }).Select(group => new WLDW
            {
                WLDWID = group.Key.WLDWID,
                WLDWCODE = group.Key.WLDWCODE,
                BANKACCLIST = group.Select(dr => new BANKACC
                {
                    ACCID = dr["ACCID"].ToString(),
                    ACCCODE = dr["ACCCODE"].ToString()
                }).ToArray()//ToList()//list形式
            });

        }
        /// <summary>
        /// 实体类转Json  简单举例
        /// </summary>
        public void PA2()
        {
            para test = new para();
            test.TestA = "A";
            test.TestB = "B";
            test.TestC = "C";

            #region Serialization
            // 方式1：Newtonsoft.Json;
            string strjson = JsonConvert.SerializeObject(test);//序列化-->实体类-Json

            //方式2：System.Web.Script.Serialization;
            JavaScriptSerializer se = new JavaScriptSerializer();

            Console.WriteLine(se.Serialize(test));
            #endregion Deserialization

            #region Deserialization
            test = JsonConvert.DeserializeObject<para>(se.Serialize(test));//反序列化-->Joson-实体类
            #endregion
        }

        /// <summary>
        /// Json转DataSet,其实就是反序列的结果，熟练使用Newtonsoft.Json;
        /// </summary>
        public void PA3()
        {
            try
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "";
                string jsonfile = "E://NotesSummary/NecessaryFile/JsonToDataSet.json";

                //读取自定目录下的json文件
                StreamReader sr = new StreamReader(jsonfile);
                string json = sr.ReadToEnd();
                Console.WriteLine(json);
                //json文件转为 对象  T 创建的类 字段名 应该和json文件中的保持一致     
                DataSet ds = JsonConvert.DeserializeObject<DataSet>(json);

                foreach (DataTable dt in ds.Tables)
                {
                    Console.WriteLine(dt.ToString());
                }

            }
            catch (Exception)
            {
                
                throw;
            }
        }
    }

    public class WLDW
    {
        public string WLDWID { get; set; }

        public string WLDWCODE { get; set; }


        public BANKACC[] BANKACCLIST { get; set; }

        //public List<BANKACC> BANKACCLIST { get; set; }//list形式


    }

    public class BANKACC
    {
        public string ACCID { get; set; }
        public string ACCCODE { get; set; }
    }

    public class para
    {
        public string TestA { get; set; }  // 测试参数A
        public string TestB { get; set; }  // 测试参数B
        public string TestC { get; set; }  // 测试参数C
    }
}
